const db = require("../config/db");

exports.getAll = async () => {
  const [rows] = await db.query("SELECT * FROM rules");
  return rows;
};

exports.add = async (rule) => {
  const { title, content } = rule;
  const [result] = await db.query(
    "INSERT INTO rules (title,content) VALUES (?,?)",
    [title, content]
  );
  return { id: result.insertId, ...rule };
};

exports.update = async (id, rule) => {
  const { title, content } = rule;
  await db.query("UPDATE rules SET title=?, content=? WHERE id=?", [
    title,
    content,
    id,
  ]);
  return { id, ...rule };
};

exports.remove = async (id) => {
  await db.query("DELETE FROM rules WHERE id=?", [id]);
};
